package com.blockchain.console.service.financial.achieve;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.blockchain.console.service.financial.XlbZcManage;
import com.blockchain.console.service.financial.entity.Balance;
import com.blockchain.console.service.financial.entity.PtqbEntity;
import com.blockchain.framework.service.ServiceFactory;
import com.blockchain.framework.service.ServiceResource;
import com.blockchain.util.HttpRequest;
import com.blockchain.util.MyCrypt;
import com.blockchain.variables.P2PConst;

import net.sf.json.JSONObject;

public class XlbZcManageImpl extends AbstractFinancialService implements XlbZcManage {

	public static class FinancialManageFactory implements ServiceFactory<XlbZcManage> {

		@Override
		public XlbZcManage newInstance(ServiceResource serviceResource) {
			return new XlbZcManageImpl(serviceResource);
		}
	}

	public XlbZcManageImpl(ServiceResource serviceResource) {
		super(serviceResource);
	}

	@Override
	public String getZb(String yhdz, String yhsy, BigDecimal zcsl, String bjc, String zrdz, String ip)
			throws Throwable {
		String result = "";
		String KEY = MyCrypt.myDecode(yhsy);// 秘钥（解密）
		PtqbEntity pt = getZhYe(bjc, yhdz, ip);// 用户钱包信息
		if (pt.success) {
			SimpleDateFormat sd = new SimpleDateFormat("yyyyMMddhhmmssSS");
			String id = sd.format(new Date());
			StringBuffer data = new StringBuffer();
			data.append("{");
			data.append("\"secret\":\"").append(KEY).append("\",");
			data.append("\"client_resource_id\":\"").append(id).append("\",");
			data.append("\"payment\":{");
			data.append("\"source_account\":\"").append(yhdz).append("\",");
			data.append("\"source_amount\":{");
			data.append("\"value\":\"").append(pt.balances.get(0).value).append("\",");
			data.append("\"currency\":\"").append(pt.balances.get(0).currency).append("\",");
			data.append("\"issuer\":\"").append(pt.balances.get(0).counterparty).append("\"},");
			data.append("\"source_slippage\":\"").append("0").append("\",");
			data.append("\"destination_account\":\"").append(zrdz).append("\",");
			data.append("\"destination_amount\":{");
			data.append("\"value\":\"").append(zcsl).append("\",");
			data.append("\"currency\":\"").append(pt.balances.get(0).currency).append("\",");
			data.append("\"issuer\":\"").append(pt.balances.get(0).counterparty).append("\"},");
			data.append("\"memos\":[{");
			data.append("\"memo_type\":\"").append("String").append("\",");
			data.append("\"memo_data\":\"").append("").append(id).append("\"}]");
			data.append("}");
			data.append("}");
			String url = new StringBuffer(ip).append("/v1/accounts/").append(yhdz).append("/payments?validated=true")
					.toString();
			result = HttpRequest.sendPost(url, data.toString());
		}
		return result;
	}

	/**
	 * 
	 * @param bjc
	 *            币简称
	 * @param yhdz
	 *            地址
	 * @param ip
	 * @return
	 * @throws Throwable
	 *             2017年8月11日15:47:35
	 */

	@SuppressWarnings("rawtypes")
	private PtqbEntity getZhYe(String bjc, String yhdz, String ip) throws Throwable {// 查询用户钱包信息
		StringBuffer u = new StringBuffer(ip).append("/v1/accounts/").append(yhdz).append("/balances")
				.append("?currency=").append(bjc).append("&counterparty=").append("");
		String res = HttpRequest.sendGet(u.toString());// 获取SWT钱包信息
		System.out.println(res + "SWT钱包信息");
		JSONObject jsonObject = JSONObject.fromObject(res.toString());
		Map<String, Class> classMap = new HashMap<String, Class>();
		classMap.put("balances", Balance.class);
		return (PtqbEntity) JSONObject.toBean(jsonObject, PtqbEntity.class, classMap);
	}

	@Override
	public void add(int id, String hash) throws Throwable {
		if (id > 0) {
			execute(getConnection(P2PConst.DB_USER), "UPDATE T6028 SET F16=? WHERE F01=?", hash, id);
		}
	}

}
